package com.taijihuabao.modules.service.impl;

import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taijihuabao.common.exception.ServiceException;
import com.taijihuabao.common.jsonresult.ApplicationResponseCode;
import com.taijihuabao.modules.entity.DeptRoleEntity;
import com.taijihuabao.modules.entity.DeptUserEntity;
import com.taijihuabao.modules.mapper.*;
import com.taijihuabao.modules.model.dept.role.DeptRoleCodeDTO;
import com.taijihuabao.modules.model.dept.role.DeptRoleDTO;
import com.taijihuabao.modules.model.dept.role.DeptRolePageDTO;
import com.taijihuabao.modules.model.dept.role.DeptRoleVO;
import com.taijihuabao.modules.model.dept.user.DeptUserDTO;
import com.taijihuabao.modules.model.user.vo.UserInfoVO;
import com.taijihuabao.modules.service.DeptRoleService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 部门角色
@author Xiao
 */
@Service
public class DeptRoleServiceImpl implements DeptRoleService {

    @Resource
    private DeptRoleMapper deptRoleMapper;

    @Resource
    private DeptMapper deptMapper;

    @Resource
    private DeptUserMapper deptUserMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private DeptRoleCodeMapper roleCodeMapper;

    @Override
    public List<DeptRoleEntity> selectDepts() {
        List<DeptRoleEntity> list = deptRoleMapper.selectDepts();
        //获取角色的名称
        if(CollectionUtils.isEmpty(list)){
            throw new ServiceException(ApplicationResponseCode.DEPT_ROLE_NOTFOUND);
        }
        for(DeptRoleEntity deptRoleEntity : list){
            String deptRole = deptRoleEntity.getDeptRole();
            if(!StringUtils.isEmpty(deptRole)){
                DeptRoleEntity roleName = deptRoleMapper.selectDeptByRoleName(deptRole);
                if(!Objects.isNull(roleName)){
                    deptRoleEntity.setRoleName(roleName.getRoleName());
                }
            }
        }
        return list;
    }

    @Override
    public List<DeptRoleVO> selectAllRole() {
        return roleCodeMapper.selectAllRole();
    }

    @Override
    public void insertRoleCode(DeptRoleCodeDTO deptRoleCodeDTO) {
        roleCodeMapper.insertRoleCode(deptRoleCodeDTO);
    }

    @Override
    public void updateRoleCode(DeptRoleCodeDTO deptRoleCodeDTO) {
        roleCodeMapper.updateRoleCode(deptRoleCodeDTO);
    }

    @Override
    public PageInfo selectAllDept(DeptRolePageDTO deptRoleDTO) {
        List<DeptRoleEntity> list = deptRoleMapper.selectAllDept(deptRoleDTO);
        //获取角色的名称
        if(CollectionUtils.isEmpty(list)){
            throw new ServiceException(ApplicationResponseCode.DEPT_ROLE_NOTFOUND);
        }
        for(DeptRoleEntity deptRoleEntity : list){
            String deptRole = deptRoleEntity.getDeptRole();
            if(!StringUtils.isEmpty(deptRole)){
                DeptRoleEntity roleName = deptRoleMapper.selectDeptByRoleName(deptRole);
                if(!Objects.isNull(roleName)){
                    deptRoleEntity.setRoleName(roleName.getRoleName());
                }
            }
        }
        PageHelper.startPage(deptRoleDTO.getPage(), deptRoleDTO.getSize());
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<DeptRoleEntity> selectDeptByCode(String deptRole) {
        List<DeptRoleEntity> list = deptRoleMapper.selectDeptByCode(deptRole);
        //获取角色的名称
        if(CollectionUtils.isEmpty(list)){
            throw new ServiceException(ApplicationResponseCode.DEPT_ROLE_NOTFOUND);
        }
        for(DeptRoleEntity deptRoleEntity : list){
            DeptRoleEntity roleName = deptRoleMapper.selectDeptByRoleName(deptRole);
            if(!Objects.isNull(roleName)){
                deptRoleEntity.setRoleName(roleName.getRoleName());
            }
        }
        return list;
    }

    @Override
    public List<DeptRoleEntity> selectDeptByDeptCode(String deptCode) {
        List<DeptRoleEntity> list = deptRoleMapper.selectDeptByDeptCode(deptCode);
        //获取角色的名称
        if(CollectionUtils.isEmpty(list)){
            throw new ServiceException(ApplicationResponseCode.DEPT_ROLE_NOTFOUND);
        }
        for(DeptRoleEntity deptRoleEntity : list){
            String deptRole = deptRoleEntity.getDeptRole();
            if(!StringUtils.isEmpty(deptRole)){
                DeptRoleEntity roleName = deptRoleMapper.selectDeptByRoleName(deptRole);
                if(!Objects.isNull(roleName)){
                    deptRoleEntity.setRoleName(roleName.getRoleName());
                }
            }
        }
        return list;
    }

    @Override
    public DeptRoleEntity selectDeptById(Long id){
        DeptRoleEntity deptRoleEntity = deptRoleMapper.selectDeptById(id);
        if(Objects.isNull(deptRoleEntity)){
            throw new ServiceException(ApplicationResponseCode.DEPT_ROLE_NOTFOUND);
        }
        return deptRoleEntity;
    }

    @Override
    public int insertDept(DeptRoleDTO deptRoleEntity) {
        //判断部门是否存在
        if(Objects.isNull(deptMapper.selectDeptByCode(deptRoleEntity.getDeptCode()))){
            throw new ServiceException(ApplicationResponseCode.DEPT_NOT_EXIST);
        }
        int result = 0;
        //判断部门角色是否存在
        DeptRoleEntity d1 = deptRoleMapper.selectDeptByCodeAndDeptCode(deptRoleEntity.getDeptRole()
                ,deptRoleEntity.getDeptCode());
        if(!Objects.isNull(d1)){
            //判断用户有没有这个部门角色，没有就新增
            insertRole(deptRoleEntity);
        }else{
            result = deptRoleMapper.insertDept(deptRoleEntity);
            if(result<1){
                throw new ServiceException(ApplicationResponseCode.DEPT_INSERT_FAILED);
            }
            //判断用户有没有这个部门角色，没有就新增
            insertRole(deptRoleEntity);
        }
        return result;
    }

    @Override
    public int updateDept(DeptRoleDTO deptRoleEntity) {
        //判断部门是否存在
        if(Objects.isNull(deptMapper.selectDeptByCode(deptRoleEntity.getDeptCode()))){
            throw new ServiceException(ApplicationResponseCode.DEPT_NOT_EXIST);
        }
        //判断角色是否存在
        DeptRoleEntity d1 = deptRoleMapper.selectDeptByCodeAndDeptCode(
                deptRoleEntity.getDeptCode(),deptRoleEntity.getDeptRole());
        if(!Objects.isNull(d1)){
            throw new ServiceException(ApplicationResponseCode.DEPT_ROLE_NOTFOUND);
        }
        int result = deptRoleMapper.updateDept(deptRoleEntity);
        if(result<1){
            throw new ServiceException(ApplicationResponseCode.DEPT_UPDATE_FAILED);
        }
        return result;
    }

    @Override
    public Integer deleteDept(DeptRoleDTO deptRoleEntity) {
        //判断角色是否存在
        DeptRoleEntity d1 = deptRoleMapper.selectDeptByCodeAndDeptCode(
                deptRoleEntity.getDeptCode(),deptRoleEntity.getDeptRole());
        if(!Objects.isNull(d1)){
            throw new ServiceException(ApplicationResponseCode.DEPTROLEID_HAS_EXIST);
        }
        int result = deptRoleMapper.deleteDept(deptRoleEntity);
        if(result<1){
            throw new ServiceException(ApplicationResponseCode.DEPT_INSERT_FAILED);
        }
        return result;
    }

    /**
     * 判断用户有没有这个部门角色，没有就新增
     * @param deptRoleEntity
     */
    public void insertRole(DeptRoleDTO deptRoleEntity){
        //判断用户有没有这个部门角色，没有就新增
        if(!CollectionUtils.isEmpty(deptRoleEntity.getUserList())){
            for(Long userId : deptRoleEntity.getUserList()){
                UserInfoVO userInfoVO = userMapper.selectById(userId);
                //判断用户是否存在
                if(Objects.isNull(userInfoVO)){
                    throw new ServiceException(ApplicationResponseCode.USER_NOT_EXIST);
                }
                DeptUserDTO d3 = new DeptUserDTO();
                if(Objects.isNull(deptUserMapper.selectDeptByUserIdAndCodeAndRole(
                        userId,deptRoleEntity.getDeptCode(),deptRoleEntity.getDeptRole()))){
                    d3.setUserId(userId);
                    d3.setDeptCode(deptRoleEntity.getDeptCode());
                    d3.setDeptRole(deptRoleEntity.getDeptRole());
                    if(1>deptUserMapper.insertDept(d3)){
                        throw new ServiceException(ApplicationResponseCode.DEPT_INSERT_FAILED);
                    }
                }
            }
        }
    }
}
